﻿var web960callback = {};
function gethostdomain() {
    var url = location.href;
    url = url.substr(url.indexOf("://") + 3);
    if (url.indexOf("/") != -1) {
        url = url.substr(0, url.indexOf('/'));
    }
    return '//' + url + '/';
}
var webhttp = gethostdomain();
function initgb(url, el) {
    if (el == undefined)
    {
        el = "#GBFORM";
    }
    if ($("#NewGBForm", el).length == 0) {
        GetGuestExtSettings(function (json) {
            var t = 0;
            if ($("#GBTYPEID", el).length != 0) {
                t = parseInt($("#GBTYPEID", el).val());
            };
            var formid = "";
            for (var i = 0; i < json.items.length; i++) {
                if (t == json.items[i].typeid) {
                    formid = json.items[i].extform;

                }
            }
            if (formid != "") {
                GetExtForms(formid, function (html) {
                    $("#gbextforms", el).html(html);
                    InitFormVerify({ el: el });
                });
            }
            else {
                InitFormVerify({ el: el });
            }
        });
        $("#GBPOST", el).click(function () {
            var title = $("#GBTITLE", el).val();
            var content = $("#GBCONTENT", el).val();

            var user = "";
            var tel = "";
            var cap = $("#GBCAPTHCA", el).val();

            if ($("#GBUSER", el).length != 0) {
                user = $("#GBUSER", el).val();
            }
            if ($("#GBTEL", el).length != 0) {
                tel = $("#GBTEL", el).val();
            }
            var im = "";
            var t = -1;
            if ($("#GBTYPEID", el).length != 0) {
                t = $("#GBTYPEID", el).val();
            }
            if ($("#GBIM", el).length != 0)
                im = $("#GBIM", el).val();

            if (title.length == 0) {
                alert(rclng.gbnotitle);
                return;
            }

            if (content.length == 0) {
                alert(rclng.gbnocontent);
                return;
            }
            var usecap = 0;
            if ($("#GBCAPTHCA", el) != null) {
                if (cap.length == 0) {
                    alert(rclng.novalidcode);
                    return;
                }
                usecap = 1
            }
          
            $("#GBPOST", el).removeClass("btndisabled").attr("disabled", true);
            jQuery.post("/index.aspx?a=g", {
                title: title,
                user: user,
                tel: tel,
                content: content,
                im: im,
                cap: cap,
                usecap: usecap,
                t: t
            }, function (ret) {
                $("#GBPOST", el).addClass("btndisabled").attr("disabled", false);
                alert(ret.message);
                if (ret.result == "1") {
                    ClearFormData({ el: el });
                }
                $("#GBCAPTCHAIMG", el).html("<img src=\"" + webhttp + "admin/createimage.aspx?rnd=" + Math.random() + "\" />");
            });
        });
        if ($("#GBCAPTCHAIMG", el) != null && $("#GBCAPTHCA", el) != null) {
            $("#GBCAPTCHAIMG", el).html("<img src=\"" + url + "admin/createimage.aspx?rnd=" + Math.random() + "\" />");
        }
    }
    else { initguestbook(url,el); }
}
function initguestbook(url,el) {
    if (el == undefined) {
        el = "#GBFORM";
    }
    GetGuestExtSettings(function (json) {
        var t = 0;
        if ($("#GBTYPEID", el).length != 0) {
            t = parseInt($("#GBTYPEID", el).val());
        };
        var formid = "";
        for (var i = 0; i < json.items.length; i++) {
            if (t == json.items[i].typeid) {
                formid = json.items[i].extform;

            }
        }
        if (formid != "") {
            GetExtForms(formid, function (html) {
                $("#gbextforms", el).html(html);
                InitFormVerify({ el: el });
            });
        }
        else {
            InitFormVerify({ el: el });
        }
    });
    $("#GBPOST", el).click(function () {

        VerifyFormData({ el: el }, function (postdata, extdata) {
        
            postdata.cap = $("#GBCAPTHCA",el).val();
            postdata.usecap = 0;
            if ($("#GBCAPTHCA", el).length > 0) {
                if (postdata.cap.length == 0) {
                    alert(rclng.novalidcode);
                    return false;
                }
                postdata.usecap = 1;
            }
            postdata.otherinfo = extdata;
            var t = -1;
            if ($("#GBTYPEID", el).length != 0) {
                t = $("#GBTYPEID", el).val();
            }
            postdata.t = t;
           
            $("#GBPOST", el).addClass("btndisabled").attr("disabled", true);
            jQuery.post("/index.aspx?a=g",
			  postdata
			, function (ret) {
			    $("#GBPOST", el).removeClass("btndisabled").attr("disabled", false);
			    alert(ret.message);
			    if (ret.result == "1") {
			        ClearFormData({ el: el });
			    }
			    $("#GBCAPTCHAIMG", el).html("<img src=\"" + webhttp + "admin/createimage.aspx?rnd=" + Math.random() + "\" />");
			   
			});
        });
    });
    if ($("#GBCAPTCHAIMG", el) != null && $("#GBCAPTHCA", el) != null) {
        $("#GBCAPTCHAIMG", el).html("<img src=\"" + url + "admin/createimage.aspx?rnd=" + Math.random() + "\" />");
    }
}
function initorder(url) {

    InitFormVerify({ el: "#ORDERFORM" });
    $("#ORDERPOST").click(function () {
        VerifyFormData({ el: "#ORDERFORM" }, function (postdata,extdata) {
            postdata.cap = $("#ORDERCAPTHCA").val();
            postdata.usecap = 0;
            if ($("#ORDERCAPTHCA") != null) {
                if (postdata.cap.length == 0) {
                    alert(rclng.novalidcode);
                    return false;
                }
                postdata.usecap = 1;
            }
            postdata.otherinfo = extdata;
           
            $("#ORDERPOST").addClass("btndisabled").attr("disabled", true);
            jQuery.post( "/index.aspx?a=order",
			  postdata
			, function (ret) {
			    $("#ORDERPOST").removeClass("btndisabled").attr("disabled", false);
			    alert(ret.message);
			    
			    $("#ORDERCAPTCHAIMG").html("<img src=\"/index.aspx?a=verifycode&verifytype=order&rnd=" + Math.random() + "\" />");
                if (ret.result == "1") {
                    try {
                        $("#ORDERFORM")[0].reset();
                    }
                    catch (e) {

                        ClearFormData({ el: "#ORDERFORM" });
                    }
			        if (typeof (Inquerycallback) != "undefined") {
			            Inquerycallback();
			        }
			    }
			    
			});
        });
    });
   
    if ($("#ORDERCAPTCHAIMG") != null && $("#ORDERCAPTHCA") != null) {
        $("#ORDERCAPTCHAIMG").html("<img src=\"/index.aspx?a=verifycode&verifytype=order\" />");
    }
}
function initInput(cfg) {
    $(cfg.el).attr("replacetxt", cfg.replacetxt).focus(function () {
        if ($(this).val() == $(this).attr("replacetxt")) {
            $(this).val("")
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("replacetxt"));
        }
    });
}
function AddToInqurieBasket(url) {
    $.get(url, {}, function (data) {
        alert(data.returnmsg);
        if (data.result == "-2") {
            location.href = data.url;
        }
    }, "json");
}
function getsearchurl()
{
    var searchurl = "index.aspx";
    try {
        if (siteurlrewriter == "1") {
            searchurl = "search.do";
        }

    }
    catch (e) { }
    return searchurl;
}
function initsimplesearch(url,el) {

   
    searchurl = getsearchurl();
    
    var postbtn;
    var PDQUERY;
    var PSIZE;
    var PDSEARCHTEMPLATE;
    var PLSEARCHQUERY;
    var PDSEARCHTYPE;
    var PDSNTYPE;
    var PDNAME;
    var PDCASNO;
    if (el != undefined) {
        postbtn = $("input[formrole='pdpost']", el);
        if (postbtn.length == 0) {
            postbtn = $("button[formrole='pdpost']", el);
        }
        PDQUERY = $("input[formrole='pdquery']", el);
        PSIZE = $("input[formrole='psize']", el);
        PDSEARCHTEMPLATE = $("input[formrole='pdsearchtemplate']", el);
        PLSEARCHQUERY = $("input[formrole='plsearchquery']", el);
        PDSEARCHTYPE = $("input[formrole='pdsearchtype']", el);
        PDSNTYPE = $("input[formrole='pdsntype']", el);
        PDNAME = $("input[formrole='pdname']", el);
        PDCASNO = $("input[formrole='pdcasno']", el);
    }
    else {
        postbtn = $("#PDPOST");
        PDQUERY = $("#PDQUERY");
        PSIZE = $("#PSIZE");
        PDSEARCHTEMPLATE = $("#PDSEARCHTEMPLATE");
        PLSEARCHQUERY = $("#PLSEARCHQUERY");
        PDSEARCHTYPE = $("#PDSEARCHTYPE");
        PDSNTYPE = $("#PDSNTYPE");
        PDNAME = $("#PDNAME");
        PDCASNO = $("#PDCASNO");
    }
 
    postbtn.click(function () {
        GoSearch();
    });

    
    PDQUERY.on("keypress", function (event) {
        if (event.keyCode == 13)
        {
            GoSearch();
        }
    });
    var GoSearch = function () {
        var q = PDQUERY.val();
        if (PDQUERY.attr("replacetxt")) {
            if (PDQUERY.attr("replacetxt") == q) {
                PDQUERY.focus();
                return false;
            }
        }
        var psize = 10;
        if (PSIZE.length == 1) {
            psize = PSIZE.val();
        }
        if (q == null || q.length == 0) {
            alert(rclng.noquerykey);
            PDQUERY.focus();
            return;
        }

        var t = -1;

        if (PDNAME.length > 0)
        {
            if (PDNAME.is(":checked"))
            {
                t = 1;
            }
        }
        if (PDCASNO.length > 0) {
            if (PDCASNO.is(":checked")) {
                t = 0;
            }
        }

        if (PDSNTYPE.length != 0) {
            t = PDSNTYPE.val();
        }
        var searchtype = 1;//1普通2批量
        if (PDSEARCHTYPE.length == 1) {
            searchtype = PDSEARCHTYPE.val();

        }
        if (searchtype == "2") {
            q = PLSEARCHQUERY.val();
        }
        var temp = "";
        if (PDSEARCHTEMPLATE.length != 0) {
            temp = PDSEARCHTEMPLATE.val();
        }
        if (searchtype == "4") {
            
            location.href = webhttp + searchurl+"?a=is&psize=" + psize + "&kw=" + encodeURIComponent(q) + "&searchtmp=" + temp;
            

        }
        else {
            location.href = webhttp + searchurl+"?a=s&searchtype=" + searchtype + "&psize=" + psize + "&q=" + encodeURIComponent(q) + "&searchtmp=" + temp + "&t=" + t;
        }
		if(event && event.preventDefault){
			event.preventDefault();
		}else{
			window.event.returnValue = false;//注意加window
		}
        
    }
}
function OpenPlLiangDialog()
{
    OpenDialog({
        el: "#PlLiangDialog",
        title: rclng.multsearch,
        width: 430
    });
}
function GoPiLiangSearch()
{
    searchurl = getsearchurl();
    var q = $.trim($("#PlSearchKeyArea").val());
    if (q != "") {
        location.href = webhttp + searchurl+"?a=s&searchtype=2&psize=50&q=" + encodeURIComponent(q) + "&searchtmp=goodssearch";
    }
    else {
        alert(rclng.casrequired);
    }
}
function initBatchSearch() {
    $(".batchsearchresultbox").html('<font color="blue">' + rclng.pending + '</font>');
    setTimeout(function () {
        goBatchSearch();
    }, 1000);
}
function goBatchSearch(callback) {
    if ($(".batchsearchitem").length > 0) {
        var pitem = $(".batchsearchitem").eq(0);
        var id = pitem.attr("refid");
        var cas = pitem.attr("cas");
        $("#batchsearchresultbox_" + id).html('<font color="green">' + rclng.searching + '......</font>');
        $.post(  "/index.aspx", { Action: "post", a: 'searchcas', searchtype: 3, q: cas },
             function (data, textStatus) {
                 // data 可以是 xmlDoc, jsonObj, html, text, 等等.
                 //this; // 这个Ajax请求的选项配置信息，请参考jQuery.get()说到的this
                 //alert(data.myRecord);
                 //alert(data.result);
                 if (callback != undefined)
                 {
                     callback(id, data);
                 }
                 else
                 {
                     var done = parseInt(data.result);
                     if (done == -1) {
                         $("#batchsearchresultbox_" + id).html('<font color="red">' + data.returnmsg + '</font>')
                     }
                     else if (done == 1) {
                         var list = data.data.list;
                         var ohtml = rclng.searchresult + "：<font color=\"red\">" + list.length + "</font>：<br /><ol>";
                         for (var i = 0; i < list.length; i++) {
                             ohtml += '<li><a href="' + list[i].url + '" target="_blank">' + list[i].pname + '</a></li>'
                         }
                         ohtml += "</ol>";
                         $("#batchsearchresultbox_" + id).html(ohtml);
                     }
                     else {
                         $("#batchsearchresultbox_" + id).html('<font color="red">' + data.returnmsg + '</font>')
                     }
                 }
                 pitem.removeClass("batchsearchitem");
                 goBatchSearch();
             }, "json");

    }
}
function initcoamsdssearch(url, el,cfg)
{
    if ($(el).length == 0) {
        return;
    }
    searchurl = getsearchurl();
    var btn = $("input[formrole='searchbtn'],button[formrole='searchbtn']", el);
    var query = $("input[formrole='query']", el);
    var prono = $("input[formrole='prono']", el);
    var no = $("input[formrole='no']", el);
    var tp = $("input[formrole='searchtype']", el);
    var typeid = $("input[formrole='typeid']", el);
    var psize = $("input[formrole='psize']", el);
    if (btn.length == 0 || query.length == 0) {
        alert("初始化initcoamsdssearch()出错");
    }
    else {
        btn.click(function () {
            GoSearch();
        });
        query.on("keypress", function (event) {
            if (event.keyCode == 13) {
                GoSearch();
            }
        });
        var GoSearch = function () {
            var q = $.trim(query.val());
            var stp = -1;
            if (tp.length > 0)
            {
                stp = tp.val();
            }

            var surl = webhttp + searchurl + "?a=coamsdssearch&typeid=" + typeid.val() + "&tp=" + stp + "&psize=" + psize.val() + "&prono=" + $.trim(prono.val()) + "&no="+$.trim(no.val())+"&q=" + encodeURIComponent(q);
            location.href = surl
        };
    }
}
function coasearch(version) {
   
    searchurl = getsearchurl();
    var nkd = $("#COASQUERY");
    var isd = $.trim(nkd.val());
    if (nkd.attr("required") != undefined) {
        if (nkd.attr("required") == "required") {
            if (isd == "" || isd == nkd.attr("replacetxt")) {
                alert(nkd.attr("replacetxt"));
                nkd.focus();
                return false;
            }
        }
    }
    var psize = 20;
    var surl = webhttp + searchurl + "?a=s&searchtmp=searchcoa&psize=" + psize + "&q=" + encodeURIComponent(isd);
    if (version != undefined) {
        if (version == 1) {
            var tp = -1;
            if ($("#COASEARCHTYPE").length > 0)
            {
                tp = $("#COASEARCHTYPE").val();
            }
            surl = webhttp + searchurl + "?a=coamsdssearch&typeid=1&tp=" + tp + "&psize=" + psize + "&q=" + encodeURIComponent(isd);
        }
    }
    location.href = surl
}
function msdssearch(version) {
    searchurl = getsearchurl();
    var nkd = $("#MSDSQUERY");
    var isd = $.trim(nkd.val());
    if (nkd.attr("required") != undefined) {
        if (nkd.attr("required") == "required") {
            if (isd == "" || isd == nkd.attr("replacetxt")) {
                alert(nkd.attr("replacetxt"));
                nkd.focus();
                return false;
            }
        }
    }
    var psize = 20;
 
    var surl =  webhttp + searchurl + "?a=s&searchtmp=searchmsds&psize=" + psize + "&q=" + encodeURIComponent(isd);
    if (version != undefined) {
        if (version == 1) {
            var tp = -1;
            if ($("#MSDSSEARCHTYPE").length > 0) {
                tp = $("#MSDSSEARCHTYPE").val();
            }
            surl = webhttp + searchurl + "?a=coamsdssearch&typeid=2&tp=" + tp + "&psize=" + psize + "&q=" + encodeURIComponent(isd);
        }
    }
    location.href = surl
}
function initadvancesearch(url) {
    $("#PDPOST").click(function () {
        //alert("高级查询");
    });
}
function viewPLSearch() {
    $("#PDQUERY").hide();
    $("#PDSNTYPE").hide();
    $("#GoPLCKBtn").hide();
    $("#PLSEARCHBOX").show();
    $("#PDSEARCHTYPE").val(2);
    $("#canPlSBtn").show();
}
function cancelPLSearch()
{
    $("#PDQUERY").show();
    $("#GoPLCKBtn").show();
    $("#PDSNTYPE").show();
    $("#canPlSBtn").hide();
    $("#PLSEARCHBOX").hide();
    $("#PDSEARCHTYPE").val(4);
}
function addtofav(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    } catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        } catch (e) {
            alert("当前浏览器不支持自动加入收藏，请手动添加。");
        }
    }
}

function setdefault(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        } else {
            alert("您的浏览器不支持，请按照下面步骤操作：1.打开浏览器设置。2.点击设置网页。3.输入：" + vrl + "点击确定。");
        }
    }
}
function initImgView() {
   // $('img[imgr]').hide();
    setTimeout(function () {
    $('img[imgr]').each(function () {

        var mh = $(this).attr("imgr").split('*');
        var ow = $(this).width();
        var oh = $(this).height();
        var nw = parseFloat(mh[0]) * 1.00;
        var nh = parseFloat(mh[1]) * 1.00;
        var oscale = ow / (oh * 1.00);
        var nscale = nw / nh;
        var tw = 0;
        var th = 0;
        if (oscale > nscale) {

            if (ow > nw) {
                tw = nw;
                th = (oh * nw) / ow;
            }
            else {
                tw = ow;
                th = oh;
            }
        }
        else {
            if (oh > nh) {
                th = nh;
                tw = (th * ow) / oh;
            }
            else {
                tw = ow;
                th = oh;
            }
        }

        $(this).width(tw).height(th).show();
    });
    }, 1000);
}
function selectItemAll(o, name) {
    var setv = false;

    if (typeof (o) == "boolean") {
        setv = o;
    }
    else {
        setv = $(o).is(":checked");
    }

    $("input[name='" + name + "']").prop("checked", setv);
}
function getSelectedValue(name) {
    var v = "";
    $("input[name='" + name + "']:checked").each(function () {
        v += $(this).val() + ",";
    });
    v = v == "" ? "" : v.substr(0, v.length - 1);
    return v;
}
function getSelectedArrayVal(name) {
    var v = [];
    $("input[name='" + name + "']:checked").each(function () {
        v.push($(this).val());
    });
    return v;
}
function deleteInquiry(url) {
    var sv = getSelectedValue("item");
    var av = getSelectedArrayVal("item");
    $.get(url, { pid: sv }, function (data) {
        if (data.result == "1") {
            for (var i = 0; i < av.length; i++) {
                $("input[name='item'][value='" + av[i] + "']").parent().parent().remove();
            }
        }
    }, "json");
}
function goOrderForm() {
    var sv = getSelectedValue("item");
    if (sv == "") {
        alert("至少选择一项");
        return false;
    }
    else {
        if (sv.indexOf(",") == -1) {
            $("#ORDERTITLE").val($("#inqueryTitle_" + sv).html());
            $("#ORDERTITLETR").show();

        }
        else {
            $("#ORDERTITLE").val("产品订单提交");
            $("#ORDERTITLETR").hide();
        }
        $("#ORDERINDEXID").val(sv);
        $("#inqurieBaskedtable").hide();
        $("#orderformBox").show();
    }
}
function getstructureimage()
{
  
    $(".structureimage").each(function () {
        var cas = $(this).attr("cas");
        var img = $(this).attr("src");
        var o = $(this);
        if (cas != "")
        {
            if (img == "" || img.indexOf('noimage') != -1)
            {
                $.post( "/index.aspx?a=getstructureimage", { cas: cas }, function (data) {
                    if (data.result == "1")
                    {
                        o.attr("src", data.url);
                    }
                }, "json");
            }
        }
    });
}